package com.gocoolmall.gateway.config;
//前端报跨域问题
//Access to XMLHttpRequest at 'http://localhost:88/api/sys/login' from origin 'http://localhost:8001' has been blocked by CORS policy:
// Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the
// requested resource.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

@Configuration
public class GocoolmallCorsConfiguration {
    @Bean  //加载到容器中
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source= new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();  //跨域配置
        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");//允许那些头
        corsConfiguration.addAllowedMethod("*");//允许请求方法，如：get、post等
        corsConfiguration.addAllowedOrigin("*");//允许请求来源
        corsConfiguration.setAllowCredentials(true);//是否允许cooke
        source.registerCorsConfiguration("/**",corsConfiguration);//请求路径，跨域条件   registerCorsConfiguration注册跨域配置

//        CorsConfigurationSource corsConfigurationSource = new CorsConfigurationSource() {
//            @Override
//            public org.springframework.web.cors.CorsConfiguration getCorsConfiguration(ServerWebExchange serverWebExchange) {
//                return null;
//            }
//        };
        return new CorsWebFilter(source);
    }
}
